package com.luck.emp.saleman.web;


import org.apache.commons.lang.StringUtils;

import com.luck.emp.entity.EmpTSalesman;
import com.luck.emp.saleman.logic.ISalemanLogic;
import com.luck.emp.saleman.qry.SalemanQry;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseActionSupport;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:ACTION类
 * Copyright: Copyright (c) 2011
 * 深圳**科技
 * @author 
 * 
 */
public class SalemanAction extends BaseActionSupport {
		/**
		 * 逻辑处理类
		 */
		ISalemanLogic salemanLogic;
		/**
		 * 查询对象
		 */
		SalemanQry qry;
		/**
		 * 分页对象
		 **/
		Page page;
		/**
		 * 实体类
		 */
		EmpTSalesman entity;

		/**
		 * 列表
		 */
		public String select() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (SalemanQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new SalemanQry();
			}
			qry.setUserInfo(userInfo);
			qry.setStatus("1");
			qry.setOrgId(userInfo.getOrgId());
			page = salemanLogic.findSalemanPage(qry);
			return "select";
		}
		/**
		 * 列表
		 */
		public String list() throws Exception{
			UserBean userInfo = super.getUserInfo();
		
			if(StringUtils.isNotBlank(super.qryHex)){
				qry = (SalemanQry)ObjectUtil.getObjectFromHex(qryHex);
			}
			if(qry==null){
				qry = new SalemanQry();
			}
			qry.setUserInfo(userInfo);
			page = salemanLogic.findSalemanPage(qry);
			return "list";
		}
		/**
		 * 新增
		 **/
		public String toAdd() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = new EmpTSalesman();
			super.event="ADD";
			super.title="新增业务员管理";
			return "edit";
		}
		/**
		 * 修改
		 **/
		public String toEdit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			entity = salemanLogic.get(EmpTSalesman.class,entity.getId());
			super.event="EDIT";
			super.title="修改业务员管理";
			return "edit";
		}
		/**
		 * 修改动作
		 **/
		public String edit() throws Exception{
			UserBean userInfo = super.getUserInfo();
			String str = "";
			try {
				if("ADD".equals(super.event)){
					//新增操作
					entity.setId(null);
					str = salemanLogic.saveSaleman(userInfo,entity);
				}else if("EDIT".equals(super.event)){
					//修改操作
					EmpTSalesman db = salemanLogic.get(EmpTSalesman.class,entity.getId());
					super.copyProperties(db, entity);
					str = salemanLogic.saveSaleman(userInfo,db);
				}
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				super.msgStr = str;
				return super.GLOBAL_ERROR;
			}else{
				super.redirectUrl = "/emp/saleman_list.action";
				return super.GLOBAL_SUCCESS;
			}
		}
		/**
		 * 删除
		 **/
		public String delete() throws Exception{			
			UserBean userBean = super.getUserInfo();
			String str = "";
			try {
				str = salemanLogic.deleteSaleman(userBean, entity);
			} catch (Exception e) {
				str = e.getMessage();
			}
			if(StringUtils.isNotBlank(str)){
				super.renderJsonError(str);
			}else
				super.renderJsonSuccess("删除成功!");
			return null;
		}
		
		/**
		 * 修改
		 **/
		public String view() throws Exception{
			entity = salemanLogic.get(EmpTSalesman.class,entity.getId());
			super.title="业务员详细信息";
			return "view";
		}
		
		public SalemanQry getQry(){
			return this.qry;
		}
		public void setQry(SalemanQry qry){
			this.qry = qry;
		}
		
		public ISalemanLogic getSalemanLogic(){
			return this.salemanLogic;
		}
		public void setSalemanLogic(ISalemanLogic salemanLogic){
			this.salemanLogic = salemanLogic;
		}
		public Page getPage() {
			return this.page;
		}
		public void setPage(Page page) {
			this.page = page;
		}
		public EmpTSalesman getEntity(){
			return entity;
		}
		public void setEntity(EmpTSalesman entity){
			this.entity = entity;
		}
}